<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>在线投稿</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <h4>欢迎你的来稿。请认真撰写稿件并经投稿单位相关负责人审核后投稿。</h4>
    <h4>1.请仔细填写，带*号的为必填项。</h4>
    <h4>2.有关全校性的通知公告、讲座预告经投稿审核后即可发布。讲座预告投稿时须以附件形式上传讲座通过审批的证明（图片等形式）</h4>
    <h4>3.新闻稿文字请直接填写，最后署名单位和图文作者；新闻图片请插入编辑器的文稿中（浏览器为兼容模式下），特殊情况可以附件形式上传。</h4>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="投稿标题" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="投稿作者" prop="author">
        <el-input v-model="ruleForm.author"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="contactNumber">
        <el-input v-model="ruleForm.contactNumber"></el-input>
      </el-form-item>
      <el-form-item label="投稿栏目" prop="submissionColumn">
        <el-select v-model="ruleForm.submissionColumn" multiple placeholder="请选择">
          <el-option v-for="item in columnListOptions"
                     :key="item.id"
                     :label="item.name"
                     :value="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="投稿部门" prop="submissionDepartment">
        <el-input v-model="ruleForm.submissionDepartment"></el-input>
      </el-form-item>
      <el-form-item label="审核人" prop="checkUser">
        <el-input v-model="ruleForm.checkUser"></el-input>
      </el-form-item>
      <el-form-item label="投稿内容" prop="content">
        <el-input type="textarea" v-model="ruleForm.content"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交稿件</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      columnListOptions: [
        {id: '1', name: '学校要闻'}, {id: '2', name: '通知公告'}, {id: '3', name: '讲座报告'},
        {id: '4', name: '综合新闻'}, {id: '5', name: '校园动态'}, {id: '6', name: '人才培养'},
        {id: '7', name: '社会服务'}, {id: '8', name: '学校影像'}, {id: '9', name: '视频新闻'},
        {id: '10', name: '学术天地'}],
      ruleForm: {
        title: '',
        author: '',
        contactNumber: '',
        submissionColumn: '',
        submissionDepartment: '',
        checkUser: '',
        content: '',
      },
      rules: {
        title: [
          {required: true, message: '请输入投稿标题', trigger: 'blur'},
          {min: 8, max: 25, message: '长度在 8 到 25 个字符', trigger: 'blur'}
        ],
        author: [
          {required: true, message: '请输入投稿作者', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        contactNumber: [
          {required: true, message: '请输入手机号码', trigger: 'blur'},
          {min: 8, max: 15, message: '长度在 8 到 15 个字符', trigger: 'blur'}
        ],
        submissionColumn: [
          {required: true, message: '请选择1个投稿栏目', trigger: 'blur'}
        ],
        submissionDepartment: [
          {required: true, message: '请输入投稿部门', trigger: 'blur'},
          {min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur'}
        ],
        checkUser: [
          {required: true, message: '请输入审核人', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        content: [
          {required: true, message: '请输入投稿内容', trigger: 'blur'},
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9800/news/add-new';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.ruleForm, {arrayFormat: 'repeat'});
          console.log('formData = ');
          console.log(formData)
          this.axios.post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log('responseBody = ');
            console.log(this.responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                message: '添加新闻成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>